﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.IO.IsolatedStorage;
using System.IO;

//using ColourAdvisorPhoneApp.ColourAdvisorServiceReference; // local host service reference
using ColourAdvisorPhoneApp.FashionColourAdvisorCloudServiceReference; // Azure cloud service reference

namespace ColourAdvisorPhoneApp
{
    public partial class FashionTrendsPage : PhoneApplicationPage
    {
        public FashionTrendsPage()
        {
            InitializeComponent();

            // Access App class for user colour type in isolated storage
            ColourTypeName colourTypeResult = (Application.Current as App).ColourType;

            // construct absolute Url to map to fashion trends image1 in blob storage
            String imageUrlFashionTrends = colourTypeResult.ToString() + "_FashionTrends1.png";
            // assign this Url to source property of image
            fashionTrendsImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://fashioncolouradvisor.blob.core.windows.net/images/" + imageUrlFashionTrends, UriKind.Absolute));

            // construct absolute Url to map to fashion trends image2 in blob storage
            String imageUrlFashionTrends2 = colourTypeResult.ToString() + "_FashionTrends2.png";
            // assign this Url to source property of image
            fashionTrendsImage2.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://fashioncolouradvisor.blob.core.windows.net/images/" + imageUrlFashionTrends2, UriKind.Absolute));

            // this is working
            //String fashionTrendsText = "trend text";
            //fashionTrendsTextBlock.Text = fashionTrendsText;
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                // create proxy
                ColourTypeServiceClient client = new ColourTypeServiceClient();

                // Event handler for when web service call is finished. Event handler calls another method to display the result
                client.DisplayFashionTrendsDescriptionCompleted += new EventHandler<DisplayFashionTrendsDescriptionCompletedEventArgs>(client_DisplayFashionTrendsDescriptionCompleted);

                // Access App class for user colour type in isolated storage
                ColourTypeName colourTypeResult = (Application.Current as App).ColourType;

                // Access App class to get the colour type id in isolated storage
                String colourType_id = (Application.Current as App).ColourTypeId;

                // work out the season name and year based on date on phone
                DateTime currentDate = DateTime.Now;
                int month = currentDate.Month;
                int year = currentDate.Year;
  
                // assign year value on phone's date to season_year
                Int16 season_year = (Int16)year;

                // assign value to season_name based on month value of date on phone
                string season_name = "";

                if (month == 1)
                {
                    season_name = "Autumn/Winter";
                }
                else if (month == 2)
                {
                    season_name = "Autumn/Winter";
                }
                else if (month == 3)
                {
                    season_name = "Spring/Summer";
                }
                else if (month == 4)
                {
                    season_name = "Spring/Summer";
                }
                else if (month == 5)
                {
                    season_name = "Spring/Summer";
                }
                else if (month == 6)
                {
                    season_name = "Spring/Summer";
                }
                else if (month == 7)
                {
                    season_name = "Spring/Summer";
                }
                else if (month == 8)
                {
                    season_name = "Spring/Summer";
                }
                else if (month == 9)
                {
                    season_name = "Autumn/Winter";
                }
                else if (month == 10)
                {
                    season_name = "Autumn/Winter";
                }
                else if (month == 11)
                {
                    season_name = "Autumn/Winter";
                }
                else if (month == 12)
                {
                    season_name = "Autumn/Winter";
                }

                // web service call, pass in details to method
                client.DisplayFashionTrendsDescriptionAsync(colourType_id, season_name, season_year);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
        }

        // method to display result of web service call
        void client_DisplayFashionTrendsDescriptionCompleted(object sender, DisplayFashionTrendsDescriptionCompletedEventArgs e)
        {
            // if there are no errors 
            if (e.Error == null)
            {
                try
                {
                    // e.Result is of type string
                    // Save this to App class for isolated storage 
                    //(Application.Current as App).ColourType = e.Result;
                    String fashionTrendsText = e.Result.ToString();
                    fashionTrendsTextBlock.Text = fashionTrendsText;
                }
                catch (ArgumentException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                // display error message
                MessageBox.Show(e.Error.ToString());
            }
        }

        private void BackButton_Click(object sender, EventArgs e)
        {
            // Navigate to previous page which displays images for colour type
            NavigationService.Navigate(new Uri("/ColourTypeImagesPage.xaml", UriKind.Relative));
        }

        private void IntroItem_Click(object sender, EventArgs e)
        {
            // Navigate to Intro page i.e. MainPage
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void DiscoverItem_Click(object sender, EventArgs e)
        {
            //Navigate to Colouring Details Page to Start "Discover My Colour Type"
            NavigationService.Navigate(new Uri("/ColouringDetailsPage.xaml", UriKind.Relative));
        }

        private void ColourTypeDescriptionItem_Click(object sender, EventArgs e)
        {
            //Navigate to My Colour Type Description page
            NavigationService.Navigate(new Uri("/ColourResultPage.xaml", UriKind.Relative));
        }

        private void ColourTypeImagesItem_Click(object sender, EventArgs e)
        {
            // Navigate to My Colour Type Images page
            NavigationService.Navigate(new Uri("/ColourTypeImagesPage.xaml", UriKind.Relative));
        }
    }
}